package com.campus_activity.campus_activity.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus_activity.campus_activity.common.PageResult;
import com.campus_activity.campus_activity.constant.HttpStatus;
import com.campus_activity.campus_activity.domain.dto.SchoolDTO;
import com.campus_activity.campus_activity.domain.dto.SchoolQueryDTO;
import com.campus_activity.campus_activity.domain.dto.SchoolUpdateDTO;
import com.campus_activity.campus_activity.domain.po.School;
import com.campus_activity.campus_activity.domain.vo.SchoolVo;
import com.campus_activity.campus_activity.exception.BusinessException;
import com.campus_activity.campus_activity.service.SchoolService;
import com.campus_activity.campus_activity.mapper.SchoolMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
* @author 73493
* @description 针对表【t_school(学校表)】的数据库操作Service实现
* @createDate 2023-04-24 17:07:07
*/
@Service
@RequiredArgsConstructor
public class SchoolServiceImpl extends ServiceImpl<SchoolMapper, School> implements SchoolService{

    private final SchoolMapper schoolMapper;

    @Override
    public boolean addSchool(SchoolDTO schoolDTO){

        //查找学校
        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(School::getSchoolName, schoolDTO.getSchoolName());

        School school = schoolMapper.selectOne(queryWrapper);

        //判断学校是否存在
        if(school!=null) {
            throw new BusinessException(HttpStatus.ERROR, "该学校已存在");
        }

        //新建学校
        school = new School();
        school.setSchoolName(schoolDTO.getSchoolName());
        school.setIntroduction(schoolDTO.getIntroduction());

        schoolMapper.insert(school);
        return true;
    }

    @Override
    public PageResult<SchoolVo> listSchool(SchoolQueryDTO schoolQueryDTO){
        //获取校名
        String schoolName = schoolQueryDTO.getSchoolName();

        //设置ipage
        IPage<SchoolVo> schoolVoIPage = new Page<>();
        schoolVoIPage.setCurrent(schoolQueryDTO.getCurrentPage());
        schoolVoIPage.setSize(schoolQueryDTO.getPageSize());

        //分页查询
        schoolVoIPage.setRecords(schoolMapper.findSchool(schoolVoIPage,schoolName));
        PageResult<SchoolVo> pageResult = new PageResult(schoolVoIPage);

        return pageResult;
    }

    @Override
    public boolean updateSchool(SchoolUpdateDTO schoolDTO){

        LambdaUpdateWrapper<School> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(School::getId, schoolDTO.getSchoolId())
                .set(School::getIntroduction, schoolDTO.getIntroduction());

        int i = schoolMapper.update(null,updateWrapper);
        if(i!=0) {
            return true;
        }else {
            return false;
        }
    }

    @Override
    public boolean deleteSchool(String schoolId){

        LambdaQueryWrapper<School> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(School::getId, schoolId);

        int i = schoolMapper.delete(queryWrapper);
        if(i!=0) {
            return true;
        }else {
            return false;
        }
    }

}




